﻿using System;
using System.Web.Security;

namespace WebPages.Admin
{
    public partial class Users : System.Web.UI.Page
    {
        const int pageSize = 25;
        int totalUsers;
        int totalPages;
        int currentPage = 1;

        void GetUsers()
        {
            NumberOfUsersOnline.Text = Membership.GetNumberOfUsersOnline().ToString();

            UserGrid.DataSource = Membership.GetAllUsers(currentPage - 1, pageSize, out totalUsers);

            NumberOfUsers.Text = totalUsers.ToString();

            totalPages = ((totalUsers - 1) / pageSize) + 1;

            // Ensure that we do not navigate past the last page of users.
            if (currentPage > totalPages)
            {
                currentPage = totalPages;
                GetUsers();
                return;
            }

            UserGrid.DataBind();
            CurrentPageLabel.Text = currentPage.ToString();
            TotalPagesLabel.Text = totalPages.ToString();

            NextButton.Visible = currentPage != totalPages;

            PreviousButton.Visible = currentPage != 1;

            NavigationPanel.Visible = totalUsers > 0;
        }

        public void NextButton_OnClick(object sender, EventArgs args)
        {
            currentPage = Convert.ToInt32(CurrentPageLabel.Text);
            currentPage++;
            GetUsers();
        }

        public void PreviousButton_OnClick(object sender, EventArgs args)
        {
            currentPage = Convert.ToInt32(CurrentPageLabel.Text);
            currentPage--;
            GetUsers();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            GetUsers();
        }


    }
}